// Copyright 2022 jeff.li. and/or its affiliates.


#include <tbir/runtime/msgpack/msgpack.h>
#include <tbir/runtime/registry.h>

namespace tbir::runtime {

    TBIR_REGISTER_GLOBAL("runtime.msgpack_dumps").set_body([](PyArgs args) -> RTValue {
        MXCHECK(args.size() == 1) << "[runtime.msgpack.dumps] Expect 1 arguments but get " << args.size();
        return serialization::msgpack_dumps(args[0]);
    });

    TBIR_REGISTER_GLOBAL("runtime.msgpack_loads").set_body([](PyArgs args) -> RTValue {
        MXCHECK(args.size() == 1) << "[runtime.msgpack.loads] Expect 1 arguments but get " << args.size();
        if (args[0].Is<string_view>()) {
            return serialization::msgpack_loads(args[0].AsNoCheck<string_view>());
        } else if (args[0].Is<unicode_view>()) {
            return serialization::msgpack_loads(UTF8Encode(args[0].AsNoCheck<unicode_view>()));
        } else {
            MXTHROW << "[runtime.msgpack.loads] Expect bytes or str but get " << args[0].type_name();
        }
        return None;
    });

}  // namespace tbir::runtime
